/***********************************************************************
 * Module:  PrescriptionDto.java
 * Author:  Vanil
 * Purpose: Defines the Class PrescriptionDto
 ***********************************************************************/
package personal;
import db.dao.*;
import db.dto.*;
import db.exceptions.*;
import db.factory.*;
import hosp.DrugDose;
import process.Operation;

import java.util.ArrayList;
import java.util.Collection;

public class Prescription {
    private PrescriptionDto dto;
    private PrescriptionDao dao;

    public Prescription(PrescriptionDto dto) {
        this.dto = dto;
        dao = PrescriptionDaoFactory.create();
    }
    public Doctor getDoctor() throws DoctorDaoException {
        DoctorDao dao = DoctorDaoFactory.create();
        DoctorDto doctorDto = dao.findByPrimaryKey(this.dto.getIdDoctor());
        return new Doctor(doctorDto);
    }
    public void setDoctor(Doctor doc) throws PrescriptionDaoException {
        dto.setIdDoctor(doc.getIdPerson());
    }
    public Patient getPatient() throws PatientDaoException {
        PatientDao dao = PatientDaoFactory.create();
        PatientDto patientDto = dao.findByPrimaryKey(this.dto.getIdPatient());
        return new Patient(patientDto);
    }
    public void setPatient(Patient pat) throws PrescriptionDaoException {
        dto.setIdPatient(pat.getIdPerson());
    }
    public String getComment()
    {
        return dto.getComment();
    }
    public void setComment(String comment) throws PrescriptionDaoException {
        dto.setComment(comment);
    }
    public void updateUpload() throws PrescriptionDaoException {
        dao.update(dto.getIdPrescription(),dto);
    }


   public java.util.Collection<DrugDose> getDrugDoses() throws DrugDoseDaoException {
      DrugDoseDao dao = DrugDoseDaoFactory.create();
       DrugDoseDto[] doseDtos = dao.findByPrescription(this.dto.getIdPrescription());
       Collection<DrugDose> drugDoses = new ArrayList<DrugDose>();
       for(DrugDoseDto drugDto:doseDtos)
            drugDoses.add(new DrugDose(drugDto));
       return drugDoses;
   }

   /**
     * @param newDrugDose */
   public void addDrugDose(DrugDose newDrugDose) throws DrugDoseDaoException {
      DrugDoseDao dao = DrugDoseDaoFactory.create();
       if( dao.findByPrimaryKey(newDrugDose.getIdDrugDose())==null)
           dao.insert(newDrugDose.getDto());
   }
   
   /**
     * @param oldDrugDose */
   public void removeDrugDose(DrugDose oldDrugDose) throws DrugDoseDaoException {
       DrugDoseDao dao = DrugDoseDaoFactory.create();
       dao.delete(oldDrugDose.getIdDrugDose());
   }
    public Collection<Operation> getOperations() throws OperationDaoException {
        OperationDao dao = OperationDaoFactory.create();
        OperationDto[] operationDtos = dao.findByPrescription(dto.getIdPrescription());
        Collection<Operation> operations = new ArrayList<Operation>();
        for(OperationDto operationDto: operationDtos)
            operations.add(new Operation(operationDto));
        return operations;
    }

}